<?php

class Application_Model_DbTable_Score extends Zend_Db_Table_Abstract
{

    protected $_name = 'ojscore';

    public function addScore(Application_Model_Score $score){
        $data = array(
            'ojuId' => $score->getUser()->getId(),
            'ojqId' => $score->getQuestion()->getId(),
            'ojsTime' => $score->getTime(),
            'ojsTimes' => $score->getTimes(),
            'ojsMemo' => $score->getMemo()
        );
        $this->insert($data);
        return $this;
    }
    
    public function updateScore(Application_Model_Score $score){
        $data = array(
            'ojuId' => $score->getUser()->getId(),
            'ojqId' => $score->getQuestion()->getId(),
            'ojsTime' => $score->getTime(),
            'ojsTimes' => $score->getTimes(),
            'ojsMemo' => $score->getMemo()
        );
        $this->update($data, 'ojsId = '.$score->getId());
        return $this;
    }
    
    public function deleteScore(int $id){
        $this->delete('ojsId = '.$id);
    }
    
    public function getScoreById(int $id){
        $row = $this->fetchRow('ojsId = '.$id);
        if(!$row)
            return null;
        $u = new Application_Model_DbTable_User();
        $q = new Application_Model_DbTable_Question();
        return new Application_Model_Score($id, $u->getUserById($row->ojuId), 
                $q->getQuestionById($row->ojqId), $row->ojsTime, $row->ojsTimes, $row->ojsMemo);
    }
    
    public function getScores(){
        $rowset = $this->fetchAll();
        $scores = array();
        $u = new Application_Model_DbTable_User();
        $q = new Application_Model_DbTable_Question();
        foreach ($rowset as $row) {
            $score = new Application_Model_Score($row->ojsId, $u->getUserById($row->ojuId), 
                $q->getQuestionById($row->ojqId), $row->ojsTime, $row->ojsTimes, $row->ojsMemo);
            $scores[] = $score;
        }
        return $scores;
    }
    
    public function getScoresByUser(Application_Model_User $user){
        $rowset = $this->fetchAll('ojuId = '.$user->getId());
        $scores = array();
        $u = new Application_Model_DbTable_User();
        $q = new Application_Model_DbTable_Question();
        foreach ($rowset as $row) {
            $score = new Application_Model_Score();
            $score->init($u->getUserById($row->ojuId), $q->getQuestionById($row->ojqId), $row->ojsTime, $row->ojsTimes);
            $scores[] = $score;
        }
        return $scores;
    }
    
    public function getScoresByQuestion(Application_Model_Question $question){
        $rowset = $this->fetchAll('ojqId = '.$question->getId());
        $scores = array();
        $u = new Application_Model_DbTable_User();
        $q = new Application_Model_DbTable_Question();
        foreach ($rowset as $row) {
            $score = new Application_Model_Score($row->ojsId, $u->getUserById($row->ojuId), 
                $q->getQuestionById($row->ojqId), $row->ojsTime, $row->ojsTimes, $row->ojsMemo);
            $scores[] = $score;
        }
        return $scores;
    }

}

